package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.CustomException;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * <p>Description: 根据ID删除分类</p>
     *
     * @author liuyiming
     * @date 2022-10-09 14:44
     * @version 1.0.0
     */
    @Override
    public void remove(Long id) {
        //1. 判断该id绑定的有没有菜品，如果有，则提示不能删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 != 0){
            //抛出业务异常，表示该id绑定的有菜品
            throw new CustomException("该分类已经已经绑定了菜品，不能删除");
        }
        //2. 判断该id绑定的有没有套餐，如果有，则提示不能删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = dishService.count(dishLambdaQueryWrapper);
        if (count2 != 0){
            //抛出业务异常，表示该id绑定的有套餐
            throw new CustomException("该分类已经已经绑定了套餐，不能删除");
        }
        //3. 删除
        super.removeById(id);
    }

}
